package com.kaka.bus.engine.amount.loanproduct;


import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.adapter.LoanProductAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.BpmList;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.LoanProApplyBean;
import com.kaka.bus.bean.ProductDetailBean;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.component.DaggerLoanComponent;
import com.kaka.bus.engine.home.main.h5.H5Activity;
import com.kaka.bus.engine.home.main.limit.LimitActivity;
import com.kaka.bus.engine.prompt.PromptActivity;
import com.kaka.bus.engine.rechange.GoRechangeActivity;
import com.kaka.bus.engine.result.ResultActivity;
import com.kaka.bus.util.U;
import com.kaka.bus.view.AuthenticationView;
import com.kaka.bus.view.MutipleLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.kaka.bus.constant.UserConstant.BS000;
import static com.kaka.bus.constant.UserConstant.BS001;
import static com.kaka.bus.constant.UserConstant.BS002;
import static com.kaka.bus.constant.UserConstant.BS003;
import static com.kaka.bus.constant.UserConstant.BS004;
import static com.kaka.bus.constant.UserConstant.BS005;
import static com.kaka.bus.constant.UserConstant.BS008;
import static com.kaka.bus.constant.UserConstant.BS011;
import static com.kaka.bus.constant.UserConstant.BS012;
import static com.kaka.bus.constant.UserConstant.BS015;
import static com.kaka.bus.constant.UserConstant.BS016;
import static com.kaka.bus.constant.UserConstant.BS017;
import static com.kaka.bus.constant.UserConstant.BS018;
import static com.kaka.bus.constant.UserConstant.BS019;

public class LoanProductActivity extends BaseActivity<LoanProductPresenter> implements LoanProductContract.View, LoanProductAdapter.OnItemClickLitener {
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.img_icon)
    ImageView img_icon;
    @BindView(R.id.tv_headline)
    TextView tv_headline;
    @BindView(R.id.tv_subhead)
    TextView tv_subhead;
    @BindView(R.id.btn_opinion)
    TextView btn_opinion;

    @BindView(R.id.AuthenticationView)
    AuthenticationView authenticationView;
    @BindView(R.id.rl_carddtail)
    RelativeLayout rl_carddtail;


    @BindView(R.id.tv_cardmoney)
    TextView tv_cardmoney;
    @BindView(R.id.img_baika_icon)
    ImageView img_baika_icon;
    @BindView(R.id.tv_baika_headline)
    TextView tv_baika_headline;
    @BindView(R.id.tv_baika_subhead)
    TextView tv_baika_subhead;
    @BindView(R.id.tv_baika_amount)
    TextView tv_baika_amount;
    @BindView(R.id.tv_baika_date)
    TextView tv_baika_date;
    @BindView(R.id.tv_amountUnit)
    TextView tv_amountUnit;
    @BindView(R.id.tv_periodUnit)
    TextView tv_periodUnit;
    @BindView(R.id.rcy)
    RecyclerView rcy;
    @BindView(R.id.tv_baika_unit)
    TextView tv_baika_unit;
    @BindView(R.id.mainlist_muti)
    MutipleLayout mainlist_muti;
    private List<ProductDetailBean.ProductBean> mProduct = new ArrayList<>();
    private int mType = 1;
    public static final String TYPE = "type";
    public static final String MONEY = "money";
    public static final String PROID = "PROID";
    public static final String BANNERID = "BANNERID";
    public static final int REQUESTCODE = 0;
    private String mMoney;
    private ProductDetailBean detailBean;
    FrameLayout jdcard_view, main_view, baika_view;
    private Intent mIntent;
    private String jdUrl;
    private int mNodeStatus = -1;
    private int mProductId;
    private LoanProductAdapter adapter;
    private boolean isGetProductId;

    @Override
    protected void initInject() {
        DaggerLoanComponent.builder().activityComponent(getActivityComponent()).build().inject(LoanProductActivity.this);
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getBpmList(getUserId(), getToken(), "");
        mPresenter.getDrainProduct(getUserId(), getToken(), Constant.PLATFORM, U.getAppInfo(this).getVersionName(), AppCenter.getTokenKey(), getIntent().getStringExtra(PROID));
    }

    @Override
    protected void initView() {
        super.initView();
        mainlist_muti.setEmptyText("暂无数据!");
        mainlist_muti.setEmptyImage(R.drawable.empty_view_kongtai);
        mainlist_muti.setStatus(MutipleLayout.Loading);
        jdcard_view = findViewById(R.id.jdcard_view);
        main_view = findViewById(R.id.main_view);
        baika_view = findViewById(R.id.baika_view);


        mMoney = getIntent().getStringExtra(MONEY);

        mIntent = new Intent();
//        jdUrl = detailBean.getJdUrl();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        rcy.setLayoutManager(gridLayoutManager);
        adapter = new LoanProductAdapter(this, this, mProduct);
        rcy.setAdapter(adapter);

    }

    @Override
    protected void initListener() {
        super.initListener();
        mToolbarBack.setOnClickListener(v -> finish());
        btn_opinion.setOnClickListener(v -> {
            mPresenter.chargeApply(getIntent().getStringExtra(PROID), getIntent().getStringExtra(BANNERID), getUserId(), "1");
            mPresenter.loadProductDetail(getUserId(), getToken(), AppCenter.getTokenKey(), Constant.PLATFORM, mProductId);
        });
        mainlist_muti.setOnReloadListener(v -> {
            mPresenter.getImmediateBtn(getUserId(), getToken(), AppCenter.getTokenKey(), Constant.PLATFORM, mProductId);
            mPresenter.getBpmList(getUserId(), getToken(), "");
            mPresenter.getDrainProduct(getUserId(), getToken(), Constant.PLATFORM, U.getAppInfo(this).getVersionName(), AppCenter.getTokenKey(), getIntent().getStringExtra(PROID));
        });
    }


    @Override
    protected void initDataOnStart() {
        super.initDataOnStart();
        if (isGetProductId) {
            mPresenter.getImmediateBtn(getUserId(), getToken(), AppCenter.getTokenKey(), Constant.PLATFORM, mProductId);
        }
    }


    @Override
    public void getLoadProductDetailSuccess(HttpWrapper<LoanProApplyBean> applyBeanHttpWrapper) {
        String status;
        String strUrl;
        if (applyBeanHttpWrapper.getData() == null) {
            showMessage(applyBeanHttpWrapper.getInfo());
            return;
        }
        status = applyBeanHttpWrapper.getData().getServerStatus();
        strUrl = applyBeanHttpWrapper.getData().getRequestUrl();

        if (Constant.TOKEN_TIMEOUT.equals(applyBeanHttpWrapper.code)) {
            U.tokenOut(this);
            return;
        }
        if (applyBeanHttpWrapper == null || applyBeanHttpWrapper.getData() == null) {
            showMessage("获取数据失败");
            return;
        }

        if (applyBeanHttpWrapper.getCode().equals("303")) {
            showMessage(applyBeanHttpWrapper.getInfo());
        } else if (applyBeanHttpWrapper.getCode().equals("207")) {
            startActivity(mIntent.setClass(LoanProductActivity.this, LimitActivity.class));
        } else if (applyBeanHttpWrapper.getCode().equals("308")) {
            Intent intent = new Intent(LoanProductActivity.this, ResultActivity.class);
            intent.putExtra(ResultActivity.RESULT_TYPE, ResultActivity.PERSON_CHECK);
            startActivity(intent);
        } else if ("300".equals(applyBeanHttpWrapper.getCode())) {
            Intent intent = new Intent(this, PromptActivity.class);
            intent.putExtra(PromptActivity.STATUTYPE, PromptActivity.REFURES);
            startActivity(intent);
//            U.showDialogJuJue(this, Constant.REFUES);
        } else if (Constant.SUCCESS_CODE.equals(applyBeanHttpWrapper.getCode())) {
            if (null != status) {
                switch (status) {
                    case BS002:
                    case BS001:
                        //待签约 -> 签约界面(H5)
                        mIntent.setClass(LoanProductActivity.this, H5Activity.class);
                        mIntent.putExtra(H5Activity.URL, strUrl);
                        startActivityForResult(mIntent, REQUESTCODE);
                        break;
                    case BS015:
                        //待缴费 -> 去支付(原生支付页面)
                        mIntent.setClass(LoanProductActivity.this, GoRechangeActivity.class);
                        mIntent.putExtra(GoRechangeActivity.TYPE, mType);
                        mIntent.putExtra(GoRechangeActivity.PRODUCTID, mProductId);
                        startActivity(mIntent);
                        finish();
                        break;
                    case BS011:
                        //发放中 -> 订单详情页(H5)
//                            mIntent.setClass(LoanProductActivity.this, H5Activity.class);
//                            mIntent.putExtra(H5Activity.URL, strUrl);
//                            startActivityForResult(mIntent, REQUESTCODE);
                        showMessage(applyBeanHttpWrapper.getInfo());
                        break;
                    case BS008:
                        //审核未通过(弹框)
//                        U.showDialogJuJue(LoanProductActivity.this, Constant.REFUES);
                        Intent intent = new Intent(this, PromptActivity.class);
                        intent.putExtra(PromptActivity.STATUTYPE, PromptActivity.REFURES);
                        startActivity(intent);
                        break;
                    case BS000:
                        mIntent.setClass(LoanProductActivity.this, H5Activity.class);
                        mIntent.putExtra(H5Activity.URL, strUrl);
                        startActivityForResult(mIntent, REQUESTCODE);
                        break;
                    case BS005:

                    case BS004:
                        //逾期未还 -> (h5支付)
                        mIntent.setClass(LoanProductActivity.this, H5Activity.class);
                        mIntent.putExtra(H5Activity.URL, strUrl);
                        startActivityForResult(mIntent, REQUESTCODE);
                        break;
                    case BS003:
                        //已签约 -> 待审核 (订单审核)
                        mIntent.setClass(LoanProductActivity.this, ResultActivity.class);
                        mIntent.putExtra(ResultActivity.RESULT_TYPE, ResultActivity.AUDIT);
                        startActivity(mIntent);

                        break;
                    case BS016:
                        //缴费中 -> 支付中
                        mIntent.setClass(LoanProductActivity.this, ResultActivity.class);
                        mIntent.putExtra(ResultActivity.RESULT_TYPE, ResultActivity.PAY);
                        startActivity(mIntent);
                        break;
                    case BS017:
                        //缴费失败 -> 支付失败
                        mIntent.setClass(LoanProductActivity.this, GoRechangeActivity.class);
                        mIntent.putExtra(GoRechangeActivity.TYPE, mType);
                        mIntent.putExtra(GoRechangeActivity.PRODUCTID, mProductId);
                        startActivity(mIntent);
                        finish();
                        break;
                    case BS018:
                        //已缴费/待放卡 -> 支付成功
                        mIntent.setClass(LoanProductActivity.this, ResultActivity.class);
                        mIntent.putExtra(ResultActivity.RESULT_TYPE, ResultActivity.WITHDRAW_SUCCESS);
                        startActivity(mIntent);
                        break;
                    case BS019:
                        //已缴费/待放卡 -> 支付成功
                        mIntent.setClass(LoanProductActivity.this, ResultActivity.class);
                        mIntent.putExtra(ResultActivity.RESULT_TYPE, ResultActivity.WAIT_LOAN);
                        startActivity(mIntent);
                        break;
                    case BS012:
                        showMessage(applyBeanHttpWrapper.getInfo());
                        break;
                    default:
                        showMessage(applyBeanHttpWrapper.getInfo());
                        break;
                }
            }
        } else {
            showMessage(applyBeanHttpWrapper.getInfo());
        }
    }

    @Override
    public void getLoadImmediateBtnSuccess(HttpWrapper<LoanProApplyBean> applyBeanHttpWrapper) {
        if (null != applyBeanHttpWrapper.getData() && !TextUtils.isEmpty(applyBeanHttpWrapper.getData().getServerName())) {
            btn_opinion.setText(applyBeanHttpWrapper.getData().getServerName());
        }
    }

    @Override
    public void getBpmListSuccess(HttpWrapper<List<BpmList>> bpmLists) {
        if (Constant.TOKEN_TIMEOUT.equals(bpmLists.code)) {
            U.tokenOut(this);
        } else if (Constant.SUCCESS_CODE.equals(bpmLists.code)) {
            for (int i = 0; i < bpmLists.getData().size(); i++) {
                if (bpmLists.getData().get(i).getNodeStatus().equals("NS002")) {
                    mNodeStatus = i;
                }
            }
            authenticationView.updateView(mNodeStatus);
        } else {
            showMessage(bpmLists.info);
        }
    }

    @Override
    public void drainProductSuccess(HttpWrapper<ProductDetailBean> wrapper) {
        mainlist_muti.setStatus(MutipleLayout.Success);
        if (null != wrapper.getData()) {
            isGetProductId = true;
            detailBean = wrapper.getData();
            rl_carddtail.setVisibility(View.GONE);
            mToolbarName.setText(detailBean.getProductName());
            tv_subhead.setText(TextUtils.isEmpty(detailBean.getProductDescription()) ? "" : detailBean.getProductDescription());
            tv_headline.setText(TextUtils.isEmpty(detailBean.getProductName()) ? "" : detailBean.getProductName());
            tv_baika_headline.setText(TextUtils.isEmpty(detailBean.getProductName()) ? "" : detailBean.getProductName());
            tv_baika_subhead.setText(TextUtils.isEmpty(detailBean.getProductDescription()) ? "" : detailBean.getProductDescription());
// mType = getIntent().getIntExtra(TYPE, 0);
            if (mType == 2) {//随心购
                mProductId = detailBean.getProductId();
                jdcard_view.setVisibility(View.VISIBLE);
                main_view.setVisibility(View.GONE);
                baika_view.setVisibility(View.GONE);
                tv_cardmoney.setText("￥" + mMoney);
                rl_carddtail.setOnClickListener(view -> {
                    if (!TextUtils.isEmpty(jdUrl)) {
                        mIntent.setClass(this, H5Activity.class);
                        mIntent.putExtra(H5Activity.URL, jdUrl);
                        startActivity(mIntent);
                    }
                });
            } else {//随心贷

                if (detailBean.getProductCount() > 0) {
                    if (detailBean.getProduct().size() == 0) {
                        showMessage("数据异常");
                        return;
                    }
                    mProduct.clear();
                    mProduct.addAll(detailBean.getProduct());
                    for (int i = 0; i < detailBean.getProduct().size(); i++) {
                        if (detailBean.getProduct().get(i).getEnabledClick().equals("1")) {
                            tv_periodUnit.setText(detailBean.getProduct().get(i).getPeriodUnit() + "");
                            tv_amountUnit.setText(detailBean.getProduct().get(i).getAmountUnit());
                            mProductId = detailBean.getProduct().get(i).getProductId();
                            break;
                        }

                    }
                    adapter.notifyDataSetChanged();
                    jdcard_view.setVisibility(View.GONE);
                    main_view.setVisibility(View.VISIBLE);
                    baika_view.setVisibility(View.GONE);
                    Glide.with(this)
                            .load(detailBean.getProductIcon()).error(R.drawable.icon_kakadef)
                            .into(img_icon);

                } else {//白卡
                    mProductId = detailBean.getProduct().get(0).getProductId();
                    jdcard_view.setVisibility(View.GONE);
                    main_view.setVisibility(View.GONE);
                    baika_view.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(detailBean.getProduct().get(0).getProductIcon()).error(R.drawable.icon_kakadef)
                            .into(img_baika_icon);
                    tv_baika_subhead.setText(detailBean.getProduct().get(0).getSubhead());
                    tv_baika_headline.setText(detailBean.getProduct().get(0).getProductName());
                    tv_baika_amount.setText(mMoney);
                    tv_baika_date.setText(detailBean.getProduct().get(0).getPeriodUnit() + "");
                    tv_baika_unit.setText(detailBean.getAmountUnit());
                }
            }


        }

    }

    @Override
    public void drainProductFail() {
        mainlist_muti.setStatus(MutipleLayout.Error);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onItemClick(View view, int position) {
        if (mProduct.get(position).getEnabledClick().equals("1")) {
            adapter.setSelection(position);
            tv_periodUnit.setText(detailBean.getProduct().get(position).getPeriodUnit() + "");
            tv_amountUnit.setText(detailBean.getProduct().get(position).getAmountUnit());
            mProductId = mProduct.get(position).getProductId();
        } else {
            showMessage("您还有一个订单正在处理");
        }


    }

    @Override
    protected int getLayoutId() {

        return R.layout.activity_loan_product;
    }
}
